package py.com.erpone.session.dm;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import py.com.erpone.entity.dm.Almacen;
import py.com.erpone.entity.dm.Empresa;


@Stateful
@Named
@LocalBean
@ConversationScoped
public class AlmacenBean {

	@PersistenceContext(type=PersistenceContextType.EXTENDED)
	private EntityManager em;
	
	@Inject
	private Conversation conversation;
	
	private Almacen almacen;
	
	
	public AlmacenBean(){
		
	}


	public Almacen getAlmacen() {
		return almacen;
	}


	public void setAlmacen(Almacen almacen) {
		this.almacen = almacen;
	}
	
	public String iniciar(){
		if(conversation.isTransient())
			conversation.begin();
		almacen=new Almacen();
		almacen.setEmpresa(new Empresa());
		return "/dm/almacen.xhtml";
	}
	
	public String iniciar(Almacen almacen){
		if(conversation.isTransient())
			conversation.begin();
		this.almacen=almacen;
		return "/dm/almacen.xhtml";
	}
	
	
	public String guardar(){
		if(almacen.getId()==null)
			em.persist(almacen);
		else
			em.merge(almacen);
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/almacenes.xhtml";
	}
	
	public String eliminar(Almacen almacen){
		almacen.setActivo(false);
		em.merge(almacen);
		if(!conversation.isTransient())
			conversation.end();
		return "/drootm/almacenes.xhtml";
	}
	
	public String cancelar(){
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/almacenes.xhtml";
	}
	
}
